package com.imc.api.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.dao.IOrderMapper;
import com.imc.api.order.pojo.*;
import com.imc.api.order.service.IOrderService;
import com.imc.core.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/*
 * 类描述：订单服务实现类
 * 开发者：YT
 * 创建日期：2018/10/28 21:41
 */
@Transactional
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    IOrderMapper orderMapper;//订单mapper

    /*
     * YT  2018/10/28 21:41
     * 方法描述：查询学校列表
     */
    @Override
    public List <Map <String, Object>> querySchoolList(String longitude,String latitude) {
        List <Map <String, Object>> queryList = orderMapper.querySchoolList(longitude,latitude);

//        if (queryList!=null&&!queryList.isEmpty()){
//            //coordinate
//            queryList.forEach(
//                    stringObjectMap -> {
//                        double longitude1 = Double.parseDouble(String.valueOf(stringObjectMap.get("longitude")));
//                        double latitude1 = Double.parseDouble(String.valueOf(stringObjectMap.get("latitude")));
//                        stringObjectMap.put("distance", MapUtils.GetDistance(latitude,longitude,latitude1,longitude1));
//                    }
//            );
//        }

        return queryList;
    }

    /*
     * YT  2018/10/28 21:41
     * 方法描述：根据学校ID和分类
     */
    @Override
    public List <CategoryDto> queryCategoryListBySchoolId(String schoolId, String cateType) {
        Map <String, Object> parms = new HashMap <>();
        parms.put("schoolId", schoolId);
        parms.put("lessonType", cateType);
        if("2".equals(cateType)){
             parms.put("cateType","lessonVid");
        }else {
            parms.put("cateType","lesson");
        }
        List <CategoryDto> queryFistList = orderMapper.queryCategoryListBySchoolId(parms);
        List <Lesson> querySeList = orderMapper.querySCategoryListBySchoolId(parms);
        List <Lesson> tmpList = null;
        for (int i = 0; i < queryFistList.size(); i++) {
            tmpList = new ArrayList <>();
            for (int j = 0; j < querySeList.size(); j++) {
                if (queryFistList.get(i).getId().equals(String.valueOf(querySeList.get(j).getCategoryId()))) {
                    tmpList.add(querySeList.get(j));
                }
            }
            queryFistList.get(i).setPCategory(tmpList);
        }
        return queryFistList;
    }

    /*
     * YT  2018/10/28 21:43
     * 方法描述：根据关键字分类ID学校ID获取课程
     */
    @Override
    public List <Lesson> queryLessonByCategoryId(String keyword, String categoryId, String schoolId) {
        return orderMapper.queryLessonByCategoryId(keyword, categoryId, schoolId);
    }

    /*
     * YT  2018/10/28 21:44
     * 方法描述：
     */
    @Override
    public Map <String, Object> queryScrCond() {
        Map <String, Object> result = new HashMap <String, Object>();
        result.put("lessonDate", orderMapper.queryLessonDataList());
        result.put("classSize", orderMapper.queryClassSizeList());
        return result;
    }

    /*
     * YT  2018/10/28 21:44
     * 方法描述：
     */
    @Override
    public PageInfo <Lesson> queryCourseBySecCond(SecCondDto secCondDto, Pageable pageable) {
        secCondDto.setSecName(secCondDto.getSecName());
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List <Lesson> lessons = orderMapper.queryCourseBySecCond(secCondDto);
        PageInfo info = new PageInfo <>(lessons);
        return info;
    }

    /*
     * YT  2018/10/28 21:44
     * 方法描述：根据课程ID和学校ID获取订单详情
     */
    @Override
    public Map <String, Object> queryDetailsByLessOnId(String lessonId, String schoolId){
        Map<String,Object>parms=new HashMap<>();
        parms.put("lessonId",lessonId);
        parms.put("schoolId",schoolId);
        Lesson lesson = orderMapper.queryLessonByLessonId(parms);
        List<LessonAtt>lessonAtts=orderMapper.queryLessonAtt(lessonId);
        lesson.setLessonAttList(lessonAtts);
        List<String>imgTmp=new ArrayList<>();
        for(LessonAtt lessonAtt:lessonAtts){
             imgTmp.add(lessonAtt.getAttachmentUrl());
        }
        lesson.setImages(imgTmp);
        List <LessonSchedule> lessonScheduleList = orderMapper.queryLessonSchByParentId(lessonId);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String newTime=df.format(new Date());
        try {
            if("1".equals(lesson.getLessonType())){
                for(int i=0;i<lessonScheduleList.size();i++){
                    if (newTime.compareTo(lessonScheduleList.get(i).getStartTime())>0) {
                        lessonScheduleList.get(i).setState("-1");
                    }
                }
            }
        }catch (Exception e){
            log.error("转换异常");
        }
        Map <String, Object> resultMap = new HashMap <>();
        resultMap.put("lesson", lesson);
        resultMap.put("lessDetate", lessonScheduleList);
        return resultMap;
    }

    @Override
    public List<Map<String,Object>> queryMealAll(String SchoolId) {
        return orderMapper.queryMealAll(SchoolId);
    }

    @Override
    public List<OrderBill> queryOrderList(OrderBillDto orderBillDto) {
        PageHelper.startPage(orderBillDto.getPage()+1, orderBillDto.getSize());
        return orderMapper.queryOrderList(orderBillDto);
    }

    @Override
    public OrderBill queryOrderById(OrderBillDto orderBillDto) {
        return orderMapper.queryOrderList(orderBillDto).get(0);
    }
}